package com.snbc.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.sql.*;
import java.util.Properties;

public class DB2Helper {  

	private static Logger logger = LogManager.getLogger(DB2Helper.class);
	public static Connection getConnection(){
		Properties pro=PropertiesUtil.getPro();
        Connection conn = null;
        try {  
            Class.forName(pro.getProperty("jdbc.driver"));
            conn = DriverManager.getConnection(pro.getProperty("jdbc.url"), pro.getProperty("jdbc.username"), pro.getProperty("jdbc.password"));
            logger.info("connect db2 successful");
        } catch (Exception e) {
        	logger.fatal("open db2 connection failed");
            e.printStackTrace();  
        }  
        return conn;  
    }  

    private static void closeAutoCloseable(AutoCloseable closse){
    	if(closse!=null){
    		try {
				closse.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    }



    

    private static void print(ResultSet rs){
    	try {
    		 ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();
			for (int i = 1; i <= columnCount; i++) {
				System.out.print(rsmd.getColumnName(i).toLowerCase()+",");
			}  
			System.out.println();
			int count=0;
			while (rs.next()){   
		        for (int i=1; i<=columnCount; i++){   
		            System.out.print(rs.getString(i) + " | ");
		        }   
		        count++;
		        System.out.println();
		    }   
			System.out.println("count="+count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    

    
}  